home *** CD-ROM | disk | FTP | other *** search
/ DOS Vuser Deluxe 2003 October / DOS Vuser Deluxe 2003 Oct - Disc 1.iso / SKIN / Le Crapouillot.wmz / WMPANImagix.JS < prev    next >
Text File  |  2002-01-19  |  16KB  |  698 lines

  1. /*
  2. WMPAniMagiX
  3. script library for animations or screenmates in WMP
  4. (c) 2002 X.Singy, http://www.singyfamily.com, xavier@singyfamily.com
  5.  
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10.  
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. Lesser General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with this library; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19. or check here: http://www.gnu.org/copyleft/lesser.html
  20. */
  21.  
  22.  
  23. /* sprite objects */
  24. /******************/
  25.  
  26. function sprite(initState, initScenario, x, y, z,minX,minY,maxX,maxY){
  27.  
  28.     this.pos = new pos(x,y,z,minX,minY,maxX,maxY);
  29.     this.status= new status( 0,initState,initScenario );
  30.  
  31. }
  32.  
  33. function pos(x,y,z,minX,minY,maxX,maxY){
  34.  
  35.     this.x=x;
  36.  
  37.     this.y=y;
  38.     this.x2 = x;
  39.     this.y2 = y;
  40.  
  41.     this.z=z;
  42.  
  43.     this.maxX = maxX;
  44.     this.maxY = maxY;
  45.     this.minX = minX;
  46.     this.minY = minY;
  47.  
  48.     this.width = 0;
  49.     this.height = 0;
  50.  
  51. }
  52.  
  53. function status(
  54.  
  55.     step_no,
  56.     current_state_name,
  57.     current_scenario_name
  58.  
  59. ){
  60.     this.step_no=step_no;
  61.  
  62.     this.current_state_name=current_state_name;
  63.  
  64.     this.current_scenario_name=current_scenario_name;
  65.     this.timeout = 0;
  66.     this.touched = false;
  67.     this.touching = new Array;
  68. }
  69.  
  70.  
  71. function state(){
  72.  
  73.     this.isReady=false;
  74.     this.z=0;
  75.     this.scenarios= new Array;
  76.     this.default_ontouch_next_states = new Array;
  77.     this.onErrorScenario= "" ;
  78.     this.absolutePos=false ;
  79.  
  80. }
  81.  
  82. function scenario(){
  83.  
  84.     this.random_delay = 0;
  85.     this.onclick_zones = new Array;
  86.     this.onmouseover_zones = new Array;
  87.     this.steps= new Array;
  88.     this.next_scenarios = new Array;
  89.     this.next_states = new Array;
  90.     this.ontop_next_states = new Array;
  91.     this.onbottom_next_states = new Array;
  92.     this.onleft_next_states = new Array;
  93.     this.onright_next_states = new Array;
  94.     this.ontouch_next_states = new Array;
  95.  
  96. }
  97.  
  98. function on_zone( x, y, dx, dy ){
  99.  
  100.     this.x=x;
  101.     this.y=y;
  102.     this.dx=dx;
  103.     this.dy=dy;
  104.     this.next_states = new Array;
  105.  
  106. }
  107.  
  108. function step( state_name, image, dx, dy, delay) {
  109.  
  110.     this.image= image;
  111.     this.delay= delay;
  112.     this.dx=dx;
  113.     this.dy=dy;
  114.  
  115. }
  116.  
  117. function next_scenario( scenario_name, probability ) {
  118.  
  119.     this.scenario_name = scenario_name;
  120.     this.probability = probability;
  121.  
  122. }
  123.  
  124. function next_state( state_name, scenario_name, probability ) {
  125.  
  126.     this.state_name = state_name;
  127.     this.scenario_name = scenario_name;
  128.     this.probability = probability;
  129.     this.action = null;
  130.  
  131. }
  132.  
  133. function success(){
  134.  
  135.     states[this.state_name].q_images_to_get--;
  136.  
  137. }
  138.  
  139.  
  140. /* anim scripts */
  141. /****************/
  142.  
  143.  
  144. function AMXrnd() {
  145.  
  146.         AMXrnd.seed = (AMXrnd.seed*9301+49297) % 233280;
  147.         return AMXrnd.seed/(233280.0);
  148.  
  149. }
  150.  
  151.  
  152.  
  153. function AMXmove(sid,sx,sy,sz){
  154.  
  155.     //printToDebug("AMXmov( "+ sid +","+ sx +","+ sy +","+ sz +")" );
  156.     evalString="AMXSubviewSprite" + sid + ".moveTo(" + sx + "," + sy + "," + AMXMoveTime+ ");" ;
  157.     //printToDebug(evalString);
  158.     eval(evalString);
  159.     return;
  160.  
  161. }
  162.  
  163.  
  164. function AMXswap(n,img){
  165.     evalString="AMXSubviewSprite" + n + ".backgroundImage='" + img+ "';" ;
  166.     //printToDebug(evalString);
  167.     eval(evalString);
  168.  
  169.     //printToDebug(" quit AMXswap( "+ n +"," + img + ")" );
  170.     return;
  171.  
  172. }
  173.  
  174.  
  175.  
  176. function getRandomSelection(objects ){
  177.     p = 0;
  178.     for (ilo=0;ilo<objects.length;ilo++) {
  179.         p += objects[ilo].probability;
  180.     }
  181.     r = AMXrnd()*p;
  182.     p=0 ;
  183.     selected = false ;
  184.     for (ilo=0; ilo<objects.length && !selected ;ilo++) {
  185.         p += objects[ilo].probability;
  186.         if (r<p) {
  187.             return ilo;
  188.         }
  189.     }
  190.  
  191.     return objects.length;
  192. }
  193.  
  194.  
  195.  
  196. function checkPos( sp ) {
  197.  
  198.     //printToDebug("checkPos( "+ sp +")" );
  199.  
  200.     cState =  sp.status.current_state_name;
  201.     cScenario = sp.status.current_scenario_name ;
  202.     with (states[ cState ].scenarios[ cScenario ] ) {
  203.     with (sp.pos) {
  204.  
  205.         found = false;
  206.         
  207.         if (x + width >maxX) {
  208.  
  209.         onborder_next_states = onright_next_states;
  210.         if ( onborder_next_states.length>0 ) found= true;
  211.  
  212.         } else
  213.  
  214.         if (x<minX) {
  215.  
  216.             onborder_next_states = onleft_next_states;
  217.             if ( onborder_next_states.length>0 ) found= true;
  218.  
  219.         }
  220.  
  221.         if (y + height >maxY) {
  222.  
  223.         if (!found)
  224.  
  225.             onborder_next_states = onbottom_next_states;
  226.  
  227.         else
  228.  
  229.             if (AMXrnd() > win_width/(win_height+win_width) ) {
  230.  
  231.             onborder2_next_states = onbottom_next_states;
  232.             if ( onborder2_next_states.length>0 ) 
  233.                 onborder_next_states = onborder2_next_states;
  234.  
  235.             } else {
  236.  
  237.             //keep previous direction
  238.  
  239.             }
  240.  
  241.         if ( onborder_next_states.length>0 ) found= true;
  242.  
  243.         } else
  244.  
  245.         if (y<minY) {
  246.  
  247.             if (!found)
  248.  
  249.             onborder_next_states = ontop_next_states;
  250.  
  251.             else
  252.  
  253.             if (AMXrnd() > win_width/(win_height+win_width) ) {
  254.  
  255.                 onborder2_next_states = ontop_next_states;
  256.                 if ( onborder2_next_states.length>0 ) 
  257.                 onborder_next_states = onborder2_next_states;
  258.  
  259.             } else {
  260.  
  261.                 //keep previous direction
  262.  
  263.             }
  264.  
  265.             if ( onborder_next_states.length>0 ) found= true;
  266.  
  267.         }
  268.         if (!found) {
  269.         //printToDebug(" - no border touched" );
  270.         return;
  271.         }
  272.  
  273.         if (changeState(sp, cState, cScenario, onborder_next_states)) return;
  274.         
  275.  
  276.     } // with sp.pos
  277.     } // with states
  278.     //printToDebug(" - done checkPos( "+ sp +")" );
  279.  
  280.     return;
  281. }
  282.  
  283.  
  284.  
  285. function checkTouch(nn){
  286.  
  287.     //printToDebug("checkTouch( "+ nn +")" );
  288.  
  289.     if (AMXq==0) return;
  290.     
  291.     checkTouchN ++;
  292.     if (checkTouchN > checkTouchEveryQ ) {
  293.  
  294.     checkTouchEveryQ = AMXq/4 + 1;
  295.     checkTouchN = 0;
  296.     for (ito=0;ito<AMXq;ito++){
  297.         with (AMXs[ito].pos) {
  298.             x2 = x + defaultSpriteWidth ;
  299.             y2 = y + defaultSpriteHeight ;
  300.         }
  301.     } 
  302.        
  303.     atouched = false;        
  304.  
  305.     for (il=0;il<AMXq;il++){    
  306.         if ( !(il == nn)  &&  (AMXs[il].status.touched == false) ) {
  307.         if (  isInBounds( AMXs[il].pos.x , AMXs[il].pos.x2, AMXs[nn].pos.x, AMXs[nn].pos.x2) ) {
  308.             if ( isInBounds( AMXs[il].pos.y , AMXs[il].pos.y2, AMXs[nn].pos.y, AMXs[nn].pos.y2) ) {
  309.             if (AMXs[nn].status.touched == false) {
  310.                 if (!atouched) doTouch(nn,il);
  311.                 doTouch(il,nn); 
  312.                 AMXs[il].status.touched = true;
  313.                 atouched = true;
  314.             }
  315.  
  316.             }
  317.         }
  318.         }
  319.  
  320.     }
  321.     AMXs[nn].status.touched = atouched;
  322.     }
  323.     return;
  324.     
  325. }
  326.  
  327.  
  328. function doTouch(tt,tt2) {
  329.  
  330.     printToDebug( tt + " touched " + tt2 );
  331.     sp = AMXs[tt];
  332.     sp2 = AMXs[tt2];
  333.  
  334.     cState =  sp.status.current_state_name;
  335.     cScenario = sp.status.current_scenario_name ;
  336.     with (states[ cState ].scenarios[ cScenario ] ) {
  337.     states2=ontouch_next_states[sp2.status.current_state_name] ;
  338. //    if (states2+"" == "undefined") {
  339.     if (!states2) {
  340.         states2 = states[ cState ].default_ontouch_next_states[sp2.status.current_state_name];
  341. //        if (states2+"" == "undefined") {
  342.         if (!states2) {
  343.         states2 = new Array;
  344.         }
  345.     }
  346.     if (changeState(sp, cState, cScenario, states2 )) {
  347. printToDebug( " - leaving ");
  348.         return;
  349.     } else {
  350.     }
  351.     }
  352. printToDebug( " - leaving ");
  353.     
  354. }
  355.  
  356. function isInBounds(l1,u1,l2,u2) {
  357.     //printToDebug("isInBounds...)" );
  358.     return ( ((l1<l2) && (l2<u1)) || ((l1< u2) && (u2<u1)) );
  359. }
  360.  
  361.  
  362. function changeState(sp, cState, cScenario, next_states_choice) {
  363.     
  364.     //printToDebug("changeState( "+ sp +","+ cState +","+ cScenario +")" );    
  365.     if (next_states_choice.length==0) return false;    
  366.     sel = getRandomSelection(next_states_choice);  
  367.     if ( cState == next_states_choice[sel].state_name && cScenario == next_states_choice[sel].scenario_name) {
  368.     return false;
  369.  
  370.     } else {
  371.  
  372.     sp.status.current_state_name = next_states_choice[sel].state_name ;
  373.     sp.status.current_scenario_name = next_states_choice[sel].scenario_name ;
  374.     sp.pos.z = states[ sp.status.current_state_name ].z;
  375.  
  376.     sp.status.step_no = 0 ;
  377. //removed for WMP
  378.     eval(next_states_choice[sel].action);
  379. //        //printToDebug(" ==> CHANGE : " +  !(cState == sp.status.current_state_name ) );
  380.     return !(cState == sp.status.current_state_name );
  381.  
  382.     } // else
  383. //    //printToDebug(" ==> no result" );
  384.     return;
  385. }
  386.  
  387.  
  388.  
  389. function animate(n){
  390.     //printToDebug("animate( "+ n +")" );
  391.  
  392.     sp = AMXs[n];
  393.     cStatus = sp.status;
  394.     cPos = sp.pos;
  395.     
  396.     cState = cStatus.current_state_name ;
  397.     cScenario = cStatus.current_scenario_name ;
  398.     
  399.     cStatus.step_no++;
  400.     
  401.     with (states[ cState ].scenarios[ cScenario ] ) {
  402.  
  403.     if (cStatus.step_no == steps.length) {
  404.  
  405.         cStatus.step_no = 0 ;
  406.  
  407.         //set next state
  408.         
  409.         if (!changeState(sp, cState, cScenario, next_states) ) {
  410.         //set next scenario
  411.  
  412.         sel = getRandomSelection(next_scenarios);
  413.  
  414.         cStatus.current_scenario_name = next_scenarios[sel].scenario_name ;
  415.         checkTouch(n);
  416.         } else {
  417.         //alert("state/scenario has changed")
  418.         }
  419.         
  420.  
  421.     }
  422.     }
  423.     
  424.     //scenario may have changed so, have to do with{} again
  425.     with (states[ cStatus.current_state_name ].scenarios[ cStatus.current_scenario_name ] ) {
  426.     
  427. printToDebug(" scenario: " + cStatus.current_scenario_name + " step no: " + cStatus.step_no + " - dx: " + steps[cStatus.step_no].dx );
  428.     if (states[ cStatus.current_state_name ].absolutePos) {
  429.         cPos.x = steps[ cStatus.step_no ].dx ;
  430.         cPos.y = steps[ cStatus.step_no ].dy ;
  431.     } else {
  432.         cPos.x = cPos.x + steps[ cStatus.step_no ].dx ;
  433.         cPos.y = cPos.y + steps[ cStatus.step_no ].dy ;
  434.     }
  435.  
  436.     //check if out of limits
  437.  
  438.     checkPos( sp );
  439.  
  440.     //AMXmove
  441.  
  442.     AMXmove( n , cPos.x, cPos.y, cPos.z );
  443.     
  444.     }
  445.     
  446.     //scenario may have changed so, have to do with{} again
  447.     with (states[ cStatus.current_state_name ].scenarios[ cStatus.current_scenario_name ] ) {
  448.     AMXswap( n , steps[ cStatus.step_no ].image );
  449.     if ( cStatus.current_scenario_name == "stop"  ) {
  450.  
  451.         AMXmove( n , -1000 , 0 );
  452.         cStatus.timeout=-1;
  453.  
  454.     } else {
  455.         cStatus.timeout= steps[ cStatus.step_no ].delay + random_delay*AMXrnd()  ;
  456.     }
  457.     
  458.  
  459.     }
  460.     //printToDebug("animate done ( "+ n +")" );
  461.     return;
  462.  
  463. }
  464.  
  465.  
  466. var threadLock = false;
  467.  
  468. function masterThread(){
  469.  
  470.     if (!initialized) {
  471.     if (debugAMX)  debugView.visible="true";
  472.     if (mustMaximize) {
  473.         view.minimize();
  474.         view.maximize();
  475.     }
  476.     initialized=true;
  477.         win_height = view.height ;
  478.         win_width = view.width ;
  479.               initAnims();
  480.  
  481.     //printToDebug('start timer interval');
  482. //      view.timerInterval=AMXinterval+50;
  483. //XVS TEST
  484.     view.timerInterval=0;
  485.     timer.moveTo(250,200,AMXinterval);
  486.     }
  487.     //printToDebug("masterThread " + threadLock + " - " + AMXrnd() );
  488.     
  489.     if (threadLock) {
  490.     //printToDebug("masterThread locked***" );
  491.     return;
  492.     }
  493.     threadLock = true;
  494.     for(mi=0;mi<AMXq;mi++) {
  495.         mcTimeout = AMXs[mi].status.timeout;
  496.               if (mcTimeout<-9999) {
  497.         //this one is stopped, do nothing
  498.         //printToDebug( "" + mi + " is stopped - do nothing" );
  499.     } else {
  500.         if (mcTimeout<1) {
  501.         //this one should be animated now
  502.         animate(mi);
  503.         } else {
  504.         //this shouldn't be animated now. Just decrease counter
  505.             //printToDebug( "" + mi + " is paused - wait" );
  506.         AMXs[mi].status.timeout = mcTimeout - AMXtimeoutDecrement;
  507.         }
  508.     }
  509.     }
  510.     timeoutCount++;
  511.     threadLock = false;
  512.     return;
  513. }
  514.  
  515. timerStatus=0;
  516.  
  517. function moveTimer(){
  518.     if (timerStatus==0) {
  519.         timerStatus=1;
  520.         timer.moveTo(-200,200,AMXinterval);
  521.     } else {
  522.         timerStatus=0;
  523.         timer.moveTo(-250,200,AMXinterval);
  524.     }
  525.     masterThread();
  526. }
  527.  
  528.  
  529.  
  530. function checkOnMouseOver(n){
  531.  
  532.     //printToDebug("checkOnMouseOver( "+ n +")" );
  533.     sp = AMXs[n];
  534.     cState = sp.status.current_state_name;
  535.     cScenario = sp.status.current_scenario_name;
  536.     
  537.     //printToDebug("   curren state/scenario: "+ cState +","+ cScenario +")" );
  538.     
  539.     with (states[ cState ].scenarios[ cScenario ] ) {
  540.     
  541.     xDelta=event.offsetX ;
  542.  
  543.     yDelta=event.offsetY ;
  544.     
  545.     if (onmouseover_zones.length == 0) {
  546.                   //printToDebug("   no zone defined" );
  547.         return
  548.     } else {
  549.  
  550.         for (i=0;i<onmouseover_zones.length;i++) {
  551.  
  552.             with ( onmouseover_zones[i] ) {
  553.                 //printToDebug("change? x,y:" + x + "," + y + " xDelta,yDelta: " + xDelta + "," + yDelta + " dx,dy : " + dx + "," + dy);
  554.  
  555.                 if ( (xDelta > x) && (yDelta > y) && (xDelta-x < dx) && (yDelta-y < dy) ) {
  556.                 //printToDebug("yes");
  557.                 changeState(sp, cState, cScenario, next_states)
  558.  
  559.                 sp.status.step_no = -1;
  560.                 sp.status.timeout = 0;
  561.                 return;
  562.  
  563.                 } 
  564.  
  565.             }
  566.  
  567.         }
  568.  
  569.     }
  570.     }
  571.     
  572. }
  573.  
  574.  
  575. function checkOnClick(n){
  576.  
  577.     //printToDebug("checkOnClick( "+ n +")" );
  578.     sp = AMXs[n];
  579.     cState = sp.status.current_state_name;
  580.     cScenario = sp.status.current_scenario_name;
  581.     with (states[ cState ].scenarios[ cScenario ] ) {
  582.         xDelta=event.offsetX ;
  583.         yDelta=event.offsetY ;
  584.     //printToDebug( cState );
  585.     //printToDebug( cScenario );
  586.     //printToDebug("x:"+ xDelta );
  587.     //printToDebug("y:"+ yDelta );
  588.  
  589.     if (onclick_zones.length == 0) {
  590.         //printToDebug("no action");
  591.         return
  592.     } else {
  593.  
  594.         for (iz=0;iz<onclick_zones.length;iz++) {
  595.  
  596.         with ( onclick_zones[iz] ) {
  597.             //printToDebug("change? x,y:" + x + "," + y + " xDelta,yDelta: " + xDelta + "," + yDelta + " dx,dy : " + dx + "," + dy);
  598.  
  599.             if ( (xDelta > x) && (yDelta > y) && (xDelta-x < dx) && (yDelta-y < dy) ) {
  600.             //printToDebug("yes");
  601.             if (changeState(sp, cState, cScenario, next_states) )
  602.  
  603.                 sp.status.step_no = -1;
  604.                 sp.status.timeout = 0;
  605.  
  606.             return;
  607.  
  608.             }
  609.  
  610.         }
  611.  
  612.         }
  613.  
  614.     }
  615.     }
  616.  
  617. }
  618.  
  619.  
  620.  
  621. function initAnims() {
  622.     //printToDebug('initanims');
  623.  
  624.  
  625.     initStates();
  626.     
  627.     //win_height = 600 ;
  628.    // win_width = 800 ;
  629.     //body_height = win_height;
  630.     //body_width = win_width ;
  631.  
  632.     setSpriteObjects();
  633.  
  634. }
  635.  
  636.  
  637. function printToDebug(m){
  638.     if (!debugAMX) return;
  639.     
  640.     debugLinesCursor++ ;
  641.     debugCount++;    
  642.     
  643.     if (!(debugLinesCursor<debugLinesMax)) debugLinesCursor=0;
  644.     debugLines[debugLinesCursor]  = debugCount +": " + m;
  645.     debugString="";
  646.     for (i=debugLinesCursor+1 ; i < debugLinesMax ; i++ ) {
  647.         debugString += debugLines[i] + "\n";
  648.     }
  649.     for (i= 0; i <debugLinesCursor+1 ; i++){
  650.         debugString += debugLines[i] + "\n";
  651.     }
  652.     
  653.     debug.value=debugString;
  654. }
  655.  
  656.  
  657. //ADDED for WMP
  658. var initialized=false;
  659.  
  660.     //initial values
  661.  
  662.     var win_height;
  663.     var win_width;    
  664.  
  665.     var images_array = new Array;
  666.     var AMXs = new Array;    
  667.  
  668.     var checkTouchEveryQ = 0;
  669.     var checkTouchN = 0;
  670.     
  671.     var noStatusChange =false;
  672.     var animStarted = false;
  673.     
  674.     var timeoutCount = 0;
  675.     
  676.     AMXrnd.today=new Date();
  677.  
  678.     AMXrnd.seed=AMXrnd.today.getTime();
  679.     
  680.     //REMOVED for WMP
  681.     //var delta_images = document.images.length;
  682.     //document.writeln(AMXspritesHTML());
  683.     
  684.     if (debugAMX) {
  685.     var debugLinesMax= 20;
  686.     var debugLines = new Array ;
  687.     var debugCount =0 ;
  688.  
  689.     for (i=0;i<debugLinesMax;i++) {
  690.         debugLines[i] = "";
  691.     }
  692.  
  693.     var debugLinesCursor=0;
  694.     }
  695.  
  696.     //REMOVED for WMP - put in the skin file
  697.     //window.onload = initAnims;
  698.